<template>
    <div class="table">
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item><i class="el-icon-lx-cascades"></i> 中奖管理</el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="primary" icon="delete" class="handle-del mr10" @click="delAll">批量删除</el-button>
                <el-input v-model="select_name" placeholder="用户昵称" class="handle-input mr10"></el-input>
                <el-button type="primary" icon="search" @click="search">搜索</el-button>
            </div>
            <el-table :data="data.slice((cur_page-1)*pagesize,cur_page*pagesize)" border class="table"
                      ref="multipleTable" @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="id">
                </el-table-column>
                <el-table-column prop="nick_name" label="昵称">
                </el-table-column>
                <el-table-column prop="show_title" label="显示标题">
                </el-table-column>
                <el-table-column prop="name" label="商品名称">
                </el-table-column>
                <el-table-column prop="img_url" label="图片">
                    <template scope="scope">
                        <img :src="scope.row.img_url" width="100%" height="100%" class="head_pic"/>
                    </template>
                </el-table-column>
                <el-table-column prop="producing_area" label="产地">
                </el-table-column>
                <el-table-column prop="unit" label="规格">
                </el-table-column>
                <el-table-column prop="grade" label="等级">
                </el-table-column>
                <el-table-column prop="status" label="状态">
                    <template slot-scope="scope">
                        <el-button :disabled="scope.row.status === 1" @click="codeEdit(scope.$index, scope.row)">
                            {{scope.row.status===1?'已兑换':'未兑换'}}
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background @current-change="handleCurrentChange" layout="prev, pager, next"
                               :total="total" :page-size="pagesize">
                </el-pagination>
            </div>
        </div>

        <el-dialog title="兑换码" :visible.sync="edit" width="30%">
            <el-form ref="form" :model="editform" label-width="60px">
                <el-form-item label="兑换码">
                    <el-input v-model="editform.code"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="Edit">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>
<script>
    export default {
        name: 'User',
        data() {
            return {
                url: './static/vuetable.json',
                tableData: [],
                cur_page: 1,
                total: 0,
                pagesize: 20,
                multipleSelection: [],
                select_name: '',
                del_list: [],
                is_search: false,
                editVisible: false,
                edit: false,
                form: {
                    id: '',
                    nick_name: '',
                    show_title: '',
                    name: '',
                    unit: '',
                    grade: '',
                    producing_area: '',
                    stock_num: '',
                    redeem_code: '',
                },
                editform: {
                    code: '',
                },
                idx: -1
            }
        },
        created() {
            this.getData();
        },
        computed: {
            data() {
                return this.tableData.filter((d) => {
                    let is_del = false;
                    for (let i = 0; i < this.del_list.length; i++) {
                        if (d.id === this.del_list[i].id) {
                            is_del = true;
                            break;
                        }
                    }
                        return d;
                })
            }
        },
        methods: {
            // 分页导航
            handleCurrentChange(val) {
                this.cur_page = val;
                this.getData();
            },

            getData() {
                this.$axios({
                    method: 'get',
                    url: 'http://local.guoxiaojian.com/admin/EatEveryDayPrize?search=' + this.select_name + '&page=' + this.cur_page + '&pageSize=' + this.pagesize,
                }).then((data) => {//这里使用了ES6的语法
                    this.total = data.data.total;
                    if(this.is_search === true){
                        this.tableData = data.data.data;
                        this.cur_page =1;
                        this.pagesize=20;
                        this.total=this.tableData.length;
                        console.log(this.total);
                        this.is_search = false;
                    }else {
                        this.tableData = this.tableData.concat(data.data.data);
                    }
                    //console.log(this.tableData)
                }).catch((data) => {
                    console.log(data)       //请求失败返回的数据
                })
            },
            search() {
                if (this.select_name === "") {
                    this.$message.warning("查询条件不能为空！");
                    return;
                }
                this.is_search = true;
                this.getData();
            },
            codeEdit(index, row) {
                this.idx = index;
                this.edit = true;
                this.editform = Object.assign({}, row);
            },
            delAll() {
                const length = this.multipleSelection.length;
                let str = '';
                this.del_list = this.del_list.concat(this.multipleSelection);
                for (let i = 0; i < length; i++) {
                    str += this.multipleSelection[i].name + ' ';
                }
                this.$message.error('删除了' + str);
                this.multipleSelection = [];
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },

            Edit() {
                let __this = this;
                let codelist = this.editform;
                this.$axios({
                    method: 'post',
                    url: 'http://local.guoxiaojian.com/admin/EatEveryDayPrize/cash',
                    data: {id: codelist.id, redeem_code: codelist.redeem_code}
                }).then((data) => {//这里使用了ES6的语法
                    if (data.data.code == 0) {
                        __this.edit = false;
                        __this.editform.status = 1;
                        __this.$set(__this.tableData, __this.idx, __this.editform);
                        __this.$message.success(`兑奖成功`);
                    } else {
                        __this.$message.error(`兑奖失败`);
                    }
                }).catch((data) => {
                    console.log(data)       //请求失败返回的数据
                })
            },
        }
    }

</script>

<style scoped>
    .handle-box {
        margin-bottom: 20px;
    }

    .handle-input {
        width: 300px;
        display: inline-block;
    }

    .del-dialog-cnt {
        font-size: 16px;
        text-align: center
    }

    .table {
        width: 100%;
        font-size: 14px;
    }

    .red {
        color: #ff0000;
    }
</style>
